<?
require_once("include/bittorrent.php");
dbconn(false);
loggedinorreturn();
stdhead("Arcade High Scores");

$games = array("Asteroids","Breakout","Invaders","Pacman","Lander","Snake","Tetris","Curveball");

print("<h1>High Scores</h1>\n");
print("<table width=60% border=1 cellspacing=0 cellpadding=10><tr><td align=center>\n\n");

for ($game=1;$game<=count($games);$game++)
{
	print("<p><table width=100%>\n<tr><td colspan=" . ($game<6 ? "3" : "4") . " align=center class=colhead><b>" . $games[$game-1] . "</b></td></tr>\n");
	print("<tr><td width=10%>Rank</td><td>Name</td>" . ($game<6 ? "" : "<td width=10%>Level</td>") . "<td width=10% align=right>Score</td></tr>\n");

	$res = query("SELECT level,score,user,username FROM highscores JOIN users ON users.id = highscores.user WHERE game = $game ORDER BY score DESC");
	while ($arr = mysql_fetch_array($res))
	{
		$rank = get_row_count("highscores", "JOIN users on users.id = highscores.user WHERE game = $game AND score > $arr[score]") + 1;
		if ($rank<6)
		{
			if (!isset($totalscore[$arr["user"]]))
				$totalscore[$arr["user"]] = 0;
			$totalscore[$arr["user"]] += (10-(($rank-1)*2));
			print("<tr" . ($arr[user]==$CURUSER[id] ? " style=\"background-color: #BBBBBB\"" : "") . ">");
			print("<td>$rank</td><td>$arr[username]</td>" . ($game<6 ? "" : "<td>$arr[level]</td>") . "<td align=right>$arr[score]</td></tr>\n");
		}
		else
		break;
	}
	print("</table></p>\n\n");
}

$maxscore = 0;
$maxscoreuser = "Unknown";
$maxplayed = 0;
$maxplayeduser = "Unknown";
$yourplayed = get_row_count("highscores", "WHERE user = $CURUSER[id]");
(isset($totalscore[$CURUSER["id"]])) ? $yourscore = $totalscore[$CURUSER["id"]] : $yourscore = 0;

$res = query("SELECT COUNT(highscores.ID) as played,user,username FROM highscores JOIN users ON users.id = highscores.user GROUP BY user");
while ($arr = mysql_fetch_array($res))
{
	if ($arr["played"]>$maxplayed)
	{
		$maxplayed = $arr["played"];
		$maxplayeduser = "<a class=altlink_black href=\"userdetails.php?id=$arr[user]\">$arr[username]</a>";
	}
	if ($totalscore[$arr["user"]]>$maxscore)
	{
		$maxscore = $totalscore[$arr["user"]];
		$maxscoreuser = "<a class=altlink_black href=\"userdetails.php?id=$arr[user]\">$arr[username]</a>";
	}
}

print("<p><b>You have played $yourplayed games</b></p>\n<p><b>Your total score is $yourscore</b></p>\n\n");
print("<table cellspacing=0 cellpadding=5>\n<tr>\n<td class=clearpadded>\n<table class=main border=1 cellspacing=0 cellpadding=5>\n");
print("<tr><td align=center><b>Most Bored Award</b></td></tr>\n<tr><td align=center>$maxplayeduser<br>$maxplayed games played</td></tr>\n");
print("</table>\n</td>\n<td class=clearpadded>\n<table class=main border=1 cellspacing=0 cellpadding=5>\n");
print("<tr><td align=center><b>Highest Score Award</b></td></tr>\n<tr><td align=center>$maxscoreuser<br>Score of $maxscore</td></tr>\n");
print("</table>\n</td>\n</tr>\n</table>\n\n");

print("</td></tr></table>\n\n");
print("<p align=center><b><a href=arcade.php>Back To Games</a></b></p>\n\n");

stdfoot();
?>
